<style type="text/css">
.pixel {
    display:block;
    position:absolute;
    width:1px;
    height:1px;
    border:0px;
    padding:0px;
    margin:0px;
}
</style>

<?php
/**
 * @author Arthur LAURENT
 * @version 18.05.2013
 */
class imageConverter {
    private $__img  = null;
    private $__size = array();

    function __construct ($file) {
        if (strpos($file, '.jpg') || strpos($file, 'jped'))
            $this->__img    = imagecreatefromjpeg($file);
        elseif (strpos($file, '.gif'))
            $this->__img    = imagecreatefromgif($file);
        elseif (strpos($file, '.png')) {
            $this->__img    = imagecreatefrompng($file);
        } else {
            throw new Excption('Image format is not valid');
        }

        $size 			= getimagesize($file);
        $this->__size 	= array(
        	'x' => $size[0],
        	'y' => $size[1],
        );
    }

    function __destruct () {
        imagedestroy($this->__img);
    }

    public function getHTML () {
        $x = 0;
        $y = 0;
        $html = '';

        for ($x = 0; $x <= $this->__size['x'] - 1; $x++) {
            $html .= $this->generatePixel($x, $y);

            if ($x == $this->__size['x'] - 1 && $y < $this->__size['y'] - 1) {
                $x = 0;
                ++$y;
            }
        }

        return $html;
    }

    public function generatePixel ($x, $y) {
        return '<span class="pixel" style="top:' . $y . 'px;left:' . $x . 'px;background-color:rgb(' . implode(',', $this->getRGB($x, $y)) . ');"></span>' . PHP_EOL;
    }

    public function getRGB ($x, $y) {
        $color = imagecolorat($this->__img, $x, $y);
        return array(
            'r' => ($color >> 16) & 0xFF,
            'g' => ($color >> 8) & 0xFF,
            'b' => $color & 0xFF,
        );
    }

    public function getSize () {
    	return $this->__size;
    }
}


$converter = new imageConverter('pattern.jpg');
echo $converter->getHTML();


